package com.example.zsfinacialmanage.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.constant.MessageConstant;
import com.example.zsfinacialmanage.other.AboutUsActivity;
import com.example.zsfinacialmanage.other.ReviseInfoActivity;
import com.example.zsfinacialmanage.utils.MyDBHelper;
import com.example.zsfinacialmanage.view.LoginActivity;


public class SysSettingActivity extends AppCompatActivity {

    TextView txt_name,txt_phone,txt_email,txt_username;
    Button btn_revise,btn_about,btn_logout,btn_exit;
    MyDBHelper myDBHelper;
    SQLiteDatabase db;
    String db_name,db_phone,db_email,myName;
    ImageView img_user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sys_setting);
        //绑定按钮
        initView();
        //显示用户信息
        displayUserInfo();
        //进入修改用户信息
        btnRevise();
        //进入关于我们页面
        btnAbout();
        //注销按钮功能实现
        btnLogout();
        //退出应用程序按钮功能实现
        btnExit();
    }

    private void initView() {
        txt_name = findViewById(R.id.txt_name_sys);
        txt_username = findViewById(R.id.txt_username_sys);
        txt_phone = findViewById(R.id.txt_phone_sys);
        txt_email = findViewById(R.id.txt_email_sys);
        btn_revise = findViewById(R.id.bt_revise_sys);
        btn_about = findViewById(R.id.bt_about_sys);
        btn_logout = findViewById(R.id.bt_logout_sys);
        btn_exit = findViewById(R.id.bt_exit_sys);
        img_user = findViewById(R.id.img_user_sys);
        myDBHelper = new MyDBHelper(SysSettingActivity.this);
        db = myDBHelper.getWritableDatabase();

        Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.user_avatar_ico); //获取Bitmap图片
        RoundedBitmapDrawable rbd = RoundedBitmapDrawableFactory.create(getResources(), src); //创建RoundedBitmapDrawable对象
        rbd.setCornerRadius(10000); //设置圆角Radius（根据实际需求）
        rbd.setAntiAlias(true); //设置抗锯齿
        img_user.setImageDrawable(rbd); //显示圆角
    }

    @SuppressLint("Range")
    private void displayUserInfo() {
        //获取数据库信息
        myName = getSharedPreferences("userinfo",0).getString("userName","");
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from tb_userinfo where name = ?",new String[]{myName});
        if (cursor.moveToNext()){
            db_name = cursor.getString(cursor.getColumnIndex("name"));
            db_phone = cursor.getString(cursor.getColumnIndex("phone"));
            db_email = cursor.getString(cursor.getColumnIndex("email"));
        }
        //显示信息
        txt_name.setText(db_name);
        txt_username.setText(db_name);
        txt_phone.setText(db_phone);
        txt_email.setText(db_email);
    }

    private void btnRevise() {
        btn_revise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SysSettingActivity.this,ReviseInfoActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void btnAbout() {
        btn_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SysSettingActivity.this, AboutUsActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void btnLogout() {
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //确认是否注销的弹窗
                AlertDialog.Builder popUps = new AlertDialog.Builder(SysSettingActivity.this);
                popUps.setTitle("账户注销");//设置对话框的标题
                popUps.setMessage("你确定注销此用户吗？注销后用户数据将不可恢复！");//设置对话框的内容
                popUps.setCancelable(true);//设置对话框可以取消
                //确定按钮的点击事件
                popUps.setPositiveButton("确定", new DialogInterface. OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //删除该用户收入详情表
                        db.delete("in_come","name = ?",new String[]{db_name});
                        //删除该用户支详情出表
                        db.delete("pay_out","name = ?",new String[]{db_name});
                        //删除该用户个人信息表
                        db.delete("tb_userinfo","name = ?",new String[]{db_name});
                        dialog.dismiss();//关闭弹窗
                        Intent intent = new Intent(SysSettingActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                        Toast.makeText(SysSettingActivity.this, MessageConstant.USER_LOGGED_OUT,Toast.LENGTH_SHORT).show();
                    }
                });
                //取消按钮的点击事件
                popUps.setNegativeButton("取消", new DialogInterface. OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //关闭对话框
                        dialog.dismiss();
                    }
                });
                //显示对话框
                popUps.show();
            }
        });
    }

    private void btnExit() {
        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SysSettingActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                System.exit(0);
            }
        });
    }
}